﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace AdminModule.CustomXAMLElements
{
    public class MyTreeView : TreeView
    {
        /// <summary>
        /// Вызывается каждый раз, когда раскрывается узел
        /// </summary>
        public event RoutedEventHandler NodeExpanded;
        /// <summary>
        /// Срабатывает, если узел раскрывается в первый раз
        /// </summary>
        public event RoutedEventHandler NodeExpandedOnce;

        private List<MyTreeViewItem> ExpandedTreeViewItems = new List<MyTreeViewItem>();

        protected override DependencyObject GetContainerForItemOverride()
        {
            MyTreeViewItem i = new MyTreeViewItem(this);
            TreeViewItemRegister(i);
            return i;
        }

        public void TreeViewItemRegister(TreeViewItem item)
        {
            item.Expanded += new RoutedEventHandler(i_Expanded);
            item.Collapsed += new RoutedEventHandler(item_Collapsed);
        }

        /// <summary>
        /// Удаляет узел из коллекции раскрытых ранее узлов (коллекция ExpandedTreeViewItems) и раскрывает узел
        /// </summary>
        /// <param name="item"></param>
        public void RaiseNodeExpanded(MyTreeViewItem item)
        {
            ExpandedTreeViewItems.Remove(item);
            item.RaiseNodeExpanded();
        }

        void item_Collapsed(object sender, RoutedEventArgs e)
        {
            this.SelectItem((sender as MyTreeViewItem).GetItem());
        }

        private void i_Expanded(object sender, RoutedEventArgs e)
        {
            this.SelectItem((sender as MyTreeViewItem).GetItem());

            if (NodeExpanded != null)
            {
                NodeExpanded(sender, e);
            }

            if (NodeExpandedOnce != null)
            {
                if(!ExpandedTreeViewItems.Contains(sender as MyTreeViewItem))
                {
                    ExpandedTreeViewItems.Add(sender as MyTreeViewItem);
                    NodeExpandedOnce(sender, e);
                }
            }
        }
    }

    public class MyTreeViewItem : TreeViewItem
    {
        private MyTreeView parentTreeViewItem { get; set; }

        public MyTreeViewItem(MyTreeView parentTreeView)
        {
            this.parentTreeViewItem = parentTreeView;
        }

        /// <summary>
        /// Вызывает событие, сообщающее о раскрытии узла
        /// </summary>
        public void RaiseNodeExpanded()
        {
            base.OnExpanded(new RoutedEventArgs());
        }

        protected override DependencyObject GetContainerForItemOverride()
        {
            MyTreeViewItem i = new MyTreeViewItem(parentTreeViewItem);
            parentTreeViewItem.TreeViewItemRegister(i);
            return i;
        }
    }
}
